package cn.sczc.jh.client.controller;

import java.util.List;

import org.apache.shiro.SecurityUtils;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;

import cn.sczc.jh.client.entity.Equipment;
import cn.sczc.jh.client.entity.User;
import cn.sczc.jh.client.service.equipment.EquipmentService;
import cn.sczc.jh.client.service.sensorHistory.SensorHistoryService;

@Controller
public class HomeViewController {

	@Autowired
	private SensorHistoryService sensorHistoryService;
	@Autowired
	private EquipmentService equipmentService;

	@RequestMapping({ "", "/login" })
	public String login() {
		return "login.html";
	}

	@GetMapping("/index")
	public String index(Model model) {
		List<String> sensorAddressList = sensorHistoryService.selectSensorAddressList();

		QueryWrapper<Equipment> queryWrapper = new QueryWrapper<Equipment>();
		queryWrapper.eq("equ_type", "JH20");
		queryWrapper.orderByDesc("update_time","create_time");
		List<Equipment> equipments = equipmentService.list(queryWrapper);
		if (equipments != null && equipments.size() > 0) {
			model.addAttribute("deviceId", equipments.get(0).getEquNo());
		}
		Subject subject = SecurityUtils.getSubject();
		User user = (User) subject.getPrincipal();
		model.addAttribute("permissionList", user.getPermissions());
		model.addAttribute("userId", user.getId().toString());
		model.addAttribute("tenantId", user.getTenantId().toString());
		model.addAttribute("userName", user.getUsername());
		model.addAttribute("teamList", user.getTeams());
		model.addAttribute("sensorAddressList", sensorAddressList);
		return "index";
	}
}
